package com.cis467.mrball;

import com.cis467.mrball.parts.Color;
import com.cis467.mrball.parts.Floor;
import com.cis467.mrball.parts.Level;
import com.cis467.mrball.parts.Wall;
import com.cis467.mrball.parts.Button;
import com.cis467.mrball.parts.Portal;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

public class ImageManager {

	int tileSize;
	int wallSize;

	public Bitmap level;

	public Bitmap[] floors = new Bitmap[4];
	public Bitmap[] hWalls = new Bitmap[2];
	public Bitmap[] vWalls = new Bitmap[2];
	public Bitmap[] buttons = new Bitmap[4];
	public Bitmap[] portals = new Bitmap[4];
	
	public Bitmap[] menuHWalls = new Bitmap[2];
	public Bitmap[] menuVWalls = new Bitmap[2];
	
	public Bitmap start;
	public Bitmap key;
	public Bitmap end;
	public Bitmap blank;
	
	public Bitmap menuStart;
	public Bitmap menuKey;
	public Bitmap menuEnd;
	public Bitmap menuSelected;
	
	public Bitmap inBetweenBall;
	
	public Drawable ball;

	private int[][] floorPixels;
	private int[][] hWallPixels;
	private int[][] vWallPixels;
	private int[][] buttonPixels;
	private int[][] portalPixels;
	
	private int[] startPixels;
	private int[] keyPixels;
	private int[] endPixels;

	public ImageManager(Context context) {

		level = BitmapFactory.decodeResource(context.getResources(),R.drawable.no_level);

		floors[Floor.PIT] = BitmapFactory.decodeResource(context.getResources(),R.drawable.floor_pit);
		floors[Floor.SLOW] = BitmapFactory.decodeResource(context.getResources(),R.drawable.floor_slow);
		floors[Floor.NORMAL] = BitmapFactory.decodeResource(context.getResources(),R.drawable.floor_normal);
		floors[Floor.FAST] = BitmapFactory.decodeResource(context.getResources(),R.drawable.floor_fast);

		hWalls[Wall.NORMAL] = BitmapFactory.decodeResource(context.getResources(),R.drawable.h_wall);
		hWalls[Wall.LASER] = BitmapFactory.decodeResource(context.getResources(),R.drawable.h_laser);

		vWalls[Wall.NORMAL] = BitmapFactory.decodeResource(context.getResources(),R.drawable.v_wall);
		vWalls[Wall.LASER] = BitmapFactory.decodeResource(context.getResources(),R.drawable.v_laser);

		buttons[Color.RED] = BitmapFactory.decodeResource(context.getResources(),R.drawable.button_red);
		buttons[Color.GREEN] = BitmapFactory.decodeResource(context.getResources(),R.drawable.button_green);
		buttons[Color.BLUE] = BitmapFactory.decodeResource(context.getResources(),R.drawable.button_blue);
		buttons[Color.YELLOW] = BitmapFactory.decodeResource(context.getResources(),R.drawable.button_yellow);

		portals[Color.RED] = BitmapFactory.decodeResource(context.getResources(),R.drawable.portal_red);
		portals[Color.GREEN] = BitmapFactory.decodeResource(context.getResources(),R.drawable.portal_green);
		portals[Color.BLUE] = BitmapFactory.decodeResource(context.getResources(),R.drawable.portal_blue);
		portals[Color.YELLOW] = BitmapFactory.decodeResource(context.getResources(),R.drawable.portal_yellow);
		
		start = BitmapFactory.decodeResource(context.getResources(),R.drawable.start);
		key = BitmapFactory.decodeResource(context.getResources(),R.drawable.key);
		end = BitmapFactory.decodeResource(context.getResources(),R.drawable.end);
		blank = BitmapFactory.decodeResource(context.getResources(),R.drawable.blank);

		menuStart = BitmapFactory.decodeResource(context.getResources(),R.drawable.start);
		menuKey = BitmapFactory.decodeResource(context.getResources(),R.drawable.key);
		menuEnd = BitmapFactory.decodeResource(context.getResources(),R.drawable.end);
		
		inBetweenBall = BitmapFactory.decodeResource(context.getResources(),R.drawable.inbetweenball);
		
		menuHWalls[Wall.NORMAL] = BitmapFactory.decodeResource(context.getResources(),R.drawable.h_wall);
		menuHWalls[Wall.LASER] = BitmapFactory.decodeResource(context.getResources(),R.drawable.h_laser);

		menuVWalls[Wall.NORMAL] = BitmapFactory.decodeResource(context.getResources(),R.drawable.v_wall);
		menuVWalls[Wall.LASER] = BitmapFactory.decodeResource(context.getResources(),R.drawable.v_laser);
		
		ball = context.getResources().getDrawable(R.drawable.mrball2);

		tileSize = hWalls[Wall.NORMAL].getWidth();
		wallSize = hWalls[Wall.NORMAL].getHeight();
	}

	public void scaleImages(int pTileSize) {

		tileSize = pTileSize;
		wallSize = (int) (tileSize * (Wall.THICKNESS/Floor.SIZE));

		level = Bitmap.createScaledBitmap(level, Level.FLOOR_COLS*tileSize, Level.FLOOR_ROWS*tileSize, true);

		floors[Floor.PIT] = Bitmap.createScaledBitmap(floors[Floor.PIT], tileSize, tileSize, true);
		floors[Floor.SLOW] = Bitmap.createScaledBitmap(floors[Floor.SLOW], tileSize, tileSize, true);
		floors[Floor.NORMAL] = Bitmap.createScaledBitmap(floors[Floor.NORMAL], tileSize, tileSize, true);
		floors[Floor.FAST] = Bitmap.createScaledBitmap(floors[Floor.FAST], tileSize, tileSize, true);

		hWalls[Wall.NORMAL] = Bitmap.createScaledBitmap(hWalls[Wall.NORMAL], tileSize, wallSize, true);
		hWalls[Wall.LASER] = Bitmap.createScaledBitmap(hWalls[Wall.LASER], tileSize, wallSize, true);

		vWalls[Wall.NORMAL] = Bitmap.createScaledBitmap(vWalls[Wall.NORMAL], wallSize, tileSize, true);
		vWalls[Wall.LASER] = Bitmap.createScaledBitmap(vWalls[Wall.LASER], wallSize, tileSize, true);

		buttons[Button.RED] = Bitmap.createScaledBitmap(buttons[Button.RED], tileSize, tileSize, true);
		buttons[Button.GREEN] = Bitmap.createScaledBitmap(buttons[Button.GREEN], tileSize, tileSize, true);
		buttons[Button.BLUE] = Bitmap.createScaledBitmap(buttons[Button.BLUE], tileSize, tileSize, true);
		buttons[Button.YELLOW] = Bitmap.createScaledBitmap(buttons[Button.YELLOW], tileSize, tileSize, true);

		portals[Portal.RED] = Bitmap.createScaledBitmap(portals[Portal.RED], tileSize, tileSize, true);
		portals[Portal.GREEN] = Bitmap.createScaledBitmap(portals[Portal.GREEN], tileSize, tileSize, true);
		portals[Portal.BLUE] = Bitmap.createScaledBitmap(portals[Portal.BLUE], tileSize, tileSize, true);
		portals[Portal.YELLOW] = Bitmap.createScaledBitmap(portals[Portal.YELLOW], tileSize, tileSize, true);
		
		start = Bitmap.createScaledBitmap(start, tileSize, tileSize, true);
		key = Bitmap.createScaledBitmap(key, tileSize, tileSize, true);
		end = Bitmap.createScaledBitmap(end, tileSize, tileSize, true);
		
		menuStart = Bitmap.createScaledBitmap(start, tileSize*2, tileSize*2, true);
		menuKey = Bitmap.createScaledBitmap(key, tileSize*2, tileSize*2, true);
		menuEnd = Bitmap.createScaledBitmap(end, tileSize*2, tileSize*2, true);

		menuHWalls[Wall.NORMAL] = Bitmap.createScaledBitmap(menuHWalls[Wall.NORMAL], tileSize, wallSize*4, true);
		menuHWalls[Wall.LASER] = Bitmap.createScaledBitmap(menuHWalls[Wall.LASER], tileSize, wallSize*4, true);

		menuVWalls[Wall.NORMAL] = Bitmap.createScaledBitmap(menuVWalls[Wall.NORMAL], wallSize*4, tileSize, true);
		menuVWalls[Wall.LASER] = Bitmap.createScaledBitmap(menuVWalls[Wall.LASER], wallSize*4, tileSize, true);
	}

	public void setupPixelArrays() {

		floorPixels = new int[4][tileSize*tileSize];
		hWallPixels = new int[2][tileSize*wallSize];
		vWallPixels = new int[2][tileSize*wallSize];
		buttonPixels = new int[4][tileSize*tileSize];
		portalPixels = new int[4][tileSize*tileSize];

		startPixels = new int[tileSize*tileSize];
		keyPixels = new int[tileSize*tileSize];
		endPixels = new int[tileSize*tileSize];
		
		// Get the pixels for the floors
		floors[Floor.PIT].getPixels(floorPixels[Floor.PIT], 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);

		floors[Floor.SLOW].getPixels(floorPixels[Floor.SLOW], 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);

		floors[Floor.NORMAL].getPixels(floorPixels[Floor.NORMAL], 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);

		floors[Floor.FAST].getPixels(floorPixels[Floor.FAST], 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);


		// Get the pixels for the hWall
		hWalls[Wall.NORMAL].getPixels(hWallPixels[Wall.NORMAL], 
				0, tileSize, 
				0, 0, 
				tileSize, wallSize);

		hWalls[Wall.LASER].getPixels(hWallPixels[Wall.LASER], 
				0, tileSize, 
				0, 0, 
				tileSize, wallSize);


		// Get the pixels for the left vWalls
		vWalls[Wall.NORMAL].getPixels(vWallPixels[Wall.NORMAL], 
				0, wallSize, 
				0, 0, 
				wallSize, tileSize);

		vWalls[Wall.LASER].getPixels(vWallPixels[Wall.LASER], 
				0, wallSize, 
				0, 0, 
				wallSize, tileSize);		


		// Get the pixels for the buttons
		buttons[Button.RED].getPixels(buttonPixels[Button.RED], 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);

		buttons[Button.GREEN].getPixels(buttonPixels[Button.GREEN], 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);

		buttons[Button.BLUE].getPixels(buttonPixels[Button.BLUE], 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);

		buttons[Button.YELLOW].getPixels(buttonPixels[Button.YELLOW], 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);


		// Get the pixels for the portals
		portals[Portal.RED].getPixels(portalPixels[Portal.RED], 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);

		portals[Portal.GREEN].getPixels(portalPixels[Portal.GREEN], 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);

		portals[Portal.BLUE].getPixels(portalPixels[Portal.BLUE], 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);

		portals[Portal.YELLOW].getPixels(portalPixels[Portal.YELLOW], 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);
		
		// Get the pixels for the start, key, end
		start.getPixels(startPixels, 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);

		key.getPixels(keyPixels, 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);

		end.getPixels(endPixels, 
				0, tileSize, 
				0, 0, 
				tileSize, tileSize);
	}

	public void renderLevel(Level pLevel) {

		level = Bitmap.createBitmap(Level.FLOOR_COLS*tileSize, Level.FLOOR_ROWS*tileSize, Bitmap.Config.ARGB_8888);

		// For each floor tile
		for(int col = 0; col < Level.FLOOR_COLS; col++)
		{
			for(int row = 0; row < Level.FLOOR_ROWS; row++)
			{
				int floorType = pLevel.floors[col][row].type;
				int topType    = pLevel.hWalls[col][row].type;
				int bottomType = pLevel.hWalls[col][row+1].type;
				int leftType   = pLevel.vWalls[col][row].type;
				int rightType  = pLevel.vWalls[col+1][row].type;
				
				// Paint the pixels into the bitmap
				level.setPixels(floorPixels[floorType], 
						0, tileSize, 
						col*tileSize, row*tileSize, 
						tileSize, tileSize);

				// Check top wall
				if(topType != Wall.NONE) {

					// Paint the pixels into the bitmap
					level.setPixels(hWallPixels[topType], 
							0, tileSize, 
							col*tileSize, row*tileSize, 
							tileSize, wallSize);
				}

				// Check bottom wall
				if(bottomType != Wall.NONE) {

					// Paint the pixels into the bitmap
					level.setPixels(hWallPixels[bottomType], 
							0, tileSize, 
							col*tileSize, row*tileSize + tileSize-wallSize, 
							tileSize, wallSize);
				}

				// Check left wall
				if(leftType != Wall.NONE) {

					// Paint the pixels into the bitmap
					level.setPixels(vWallPixels[leftType], 
							0, wallSize, 
							col*tileSize, row*tileSize, 
							wallSize, tileSize);
				}

				// Check right wall
				if(rightType != Wall.NONE) {

					// Paint the pixels into the bitmap
					level.setPixels(vWallPixels[rightType], 
							0, wallSize, 
							col*tileSize + tileSize-wallSize, row*tileSize, 
							wallSize, tileSize);
				}
			}
		}
	}
}
